package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.TrainScoreDao;
import cn.edu.scut.sys.service.TrainScoreService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.TrainScore;

@Component("trainScoreService")
public class TrainScoreServiceImpl implements TrainScoreService{

private TrainScoreDao trainScoreDao;
	
	@Resource(name="trainScoreDao")
	public void setUserDao(TrainScoreDao trainScoreDao) {
		this.trainScoreDao = trainScoreDao;
	}

	@Override
	public DataResponse<TrainScore> list(DataRequest request) {
		DataResponse<TrainScore> response = new DataResponse<TrainScore>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<TrainScore> list;  
          
        count = trainScoreDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        list = trainScoreDao.list(start, limit, request.getSord(), request.getSidx());  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public DataResponse<TrainScore> list(DataRequest request,String studentIdStr) {
		DataResponse<TrainScore> response = new DataResponse<TrainScore>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<TrainScore> list;  
        
        count = trainScoreDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        Long studentId = 0L;
        try{
        	
        	studentId = Long.parseLong(studentIdStr);
        } catch(Exception ex) {
        	studentId = 0L;
        }  
        
        
        list = trainScoreDao.list(start, limit, request.getSord(), request.getSidx(),studentId);  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}
	
	
	@Override
	public boolean add(TrainScore trainScore) {
		trainScoreDao.insert(trainScore);
		return true;
	}

	@Override
	public TrainScore get(Long id) {
		return trainScoreDao.get(id);
	}

	@Override
	public boolean update(TrainScore trainScore) {
		trainScoreDao.update(trainScore);
		return true;
	}
}
